rm(list = setdiff(ls(), lsf.str()))

#Load data
load("Study 1/Original Data (shareable)/Study1_UK_BES_analysisdata.Rdata")

###Restrict data to England-------------
data_BES<-subset(data_BES, country=="England")

#Voted yes/no
data_BES$didnotvote<-ifelse(data_BES$genElecTurnoutRetro=="No, did not vote", 1,0)

#recode personality
data_BES$open <-data_BES$personality_openness
data_BES$con <- data_BES$personality_conscientiousness
data_BES$ext <- data_BES$personality_extraversion
data_BES$agre <- data_BES$personality_agreeableness
data_BES$neu <- data_BES$personality_neuroticism

#sex: female ==1
data_BES$female<-ifelse(data_BES$gender=="Female", 1,0)

#age
data_BES$Age <-car::recode(as.numeric(data_BES$Age),"16=17; NA=17", as.numeric=T)

data_BES$Age_missing<-ifelse(data_BES$Age==17, 1,0)
###*cynicism
data_BES$w6_pol_cynicism <-car::recode(as.numeric(data_BES$efficacyPolCare), "6=NA", as.numeric=T)

###*redistribution attitudes
data_BES$w6_redistribution <-car::recode(as.numeric(data_BES$redistSelf), "12=NA", as.numeric=T)

###Auth
data_BES$au1<-ifelse(as.numeric(data_BES$auth1)==2, 1,
                     ifelse(as.numeric(data_BES$auth1)==1,0, NA))
data_BES$au2<-ifelse(as.numeric(data_BES$auth2)==1, 1,
                     ifelse(as.numeric(data_BES$auth2)==2,0, NA))
data_BES$au3<-ifelse(as.numeric(data_BES$auth3)==2, 1,
                     ifelse(as.numeric(data_BES$auth3)==1,0, NA))
data_BES$au4<-ifelse(as.numeric(data_BES$auth4)==2, 1,
                     ifelse(as.numeric(data_BES$auth4)==1,0, NA))

#create Authoritarianism scale
data_BES$auth_index<-(rowMeans(data.frame(data_BES$au1, data_BES$au2, data_BES$au3, data_BES$au4), na.rm=T))

#Income
data_BES$income<-as.numeric(data_BES$profile_gross_household)         
data_BES$income<-zero1(car::recode(data_BES$income, "16=NA; 17=NA"))
data_BES$income[is.na(data_BES$income)==TRUE]=2
data_BES$income_missing<-ifelse(data_BES$income==2, 1,0)

#income for descriptives
data_BES$income_descriptives<-car::recode(as.numeric(data_BES$profile_gross_household), "16=NA; 17=NA")

#create education dummy's
data_BES$education<-as.numeric(data_BES$edlevel) 
data_BES$Ed_NoQual <- ifelse(data_BES$education==1,1,0)
data_BES$Ed_GSCE_DG <- ifelse(data_BES$education==2,1,0)
data_BES$Ed_GSCE_AC <- ifelse(data_BES$education==3,1,0)
data_BES$Ed_A_level <- ifelse(data_BES$education==4,1,0)
data_BES$Ed_Undergraduate <- ifelse(data_BES$education==5,1,0)
data_BES$Ed_Postgrad <- ifelse(data_BES$education==6,1,0)

#like parties
data_BES$likeCon<-car::recode(as.numeric(data_BES$likeCon),"12=NA")
data_BES$likeLab<-car::recode(as.numeric(data_BES$likeLab),"12=NA")
data_BES$likeUKIP<-car::recode(as.numeric(data_BES$likeUKIP),"12=NA")
data_BES$likeBNP<-car::recode(as.numeric(data_BES$likeBNP),"12=NA")
data_BES$likeLD<-car::recode(as.numeric(data_BES$likeLD),"12=NA")
data_BES$likeGrn<-car::recode(as.numeric(data_BES$likeGrn),"12=NA")

#Dummy voting for UKIP
data_BES$w6_voteUKIP<-ifelse(as.numeric(data_BES$generalElectionVote)==7,1,
                             ifelse(as.numeric(data_BES$generalElectionVote)==5 | as.numeric(data_BES$generalElectionVote)==6, NA, 0))

#UKIP, other parties, not voting
data_BES$populist_other_not<-data_BES$w6_voteUKIP
data_BES$populist_other_not[data_BES$didnotvote==1]=2

#UKIP; Government (conservatives & Lib Dems); opposition (Other); don't know -----
data_BES$w6_UKIP_gov_opposition<-car::recode(as.numeric(data_BES$generalElectionVote), "2=1; 3=2; 4=1; 5=NA; 6=1; 7=0; 8=2; 9=2; 10=2; 11=3")
data_BES$w6_UKIP_gov_opposition<-as.factor(data_BES$w6_UKIP_gov_opposition)
levels(data_BES$w6_UKIP_gov_opposition) <- c("UKIP","Government","Opposition", "Don't know")
data_BES$w6_UKIP_gov_opposition <- relevel(data_BES$w6_UKIP_gov_opposition, ref = "UKIP")

###immigrant attitudes W4
data_BES$w4_immigCultural <- data_BES$immigCultural
data_BES$w4_immigEcon <- data_BES$immigEcon
data_BES$w4_immigrantsWelfareState <-data_BES$immigrantsWelfareState

#recode to recode reversed coded items; set 9999 to missing and score from 0-1
data_BES$w4_immigCultural_rec <-car::recode(as.numeric(data_BES$w4_immigCultural),"1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; 8=NA", as.numeric=T)
data_BES$w4_immigCultural_rec<-(data_BES$w4_immigCultural_rec-1)/6
data_BES$w4_immigEcon_rec <-car::recode(as.numeric(data_BES$w4_immigEcon),"1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; 8=NA", as.numeric=T)
data_BES$w4_immigEcon_rec<-(data_BES$w4_immigEcon_rec-1)/6
data_BES$w4_immigrantsWelfareState <-car::recode(as.numeric(data_BES$w4_immigrantsWelfareState),"6=NA", as.numeric=T)
data_BES$w4_immigrantsWelfareState<-(data_BES$w4_immigrantsWelfareState-1)/4
data_BES$w4_immiatt<-(rowMeans(data.frame(data_BES$w4_immigCultural_rec, data_BES$w4_immigEcon_rec, data_BES$w4_immigrantsWelfareState), na.rm=T))

#Code left-right
data_BES$lr_ideology<-car::recode(as.numeric(data_BES$leftRight), "12=NA")

#save data
save(data_BES, file="Study 1/Altered Data/Study1_UK_BES.RData")
